<%@page import="qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.model.QuanHuyen"%>
<%@page import="qldc.pm.tienan.constants.ThaoTacUndoRedoEnum"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="qldc.pm.tienan.service.TEMP_DoiTuongTienAnLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.TEMP_DoiTuongTienAn"%>
<%@page import="qldc.pm.tienan.service.TEMP_TienAnLocalServiceUtil"%>
<%@page import="qldc.pm.tienan.model.TEMP_TienAn"%>
<%@page import="qldc.pm.tienan.constants.TrangThaiTienAnTrongThongKeEnum"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="qldc.pm.tienan.dto.DoiTuongTienAnDTO"%>
<%@page import="qldc.pm.tienan.service.DoiTuongTienAnDTOLocalServiceUtil"%>
<%@page import="qldc.pm.qtht.dmdc.utils.DiaChiUtils"%>
<%@page import="qldc.pm.tienan.searchcontainer.NguoiCoTienAnDisplayTerms"%>
<%@page import="qldc.pm.tienan.searchcontainer.NguoiCoTienAnSearch"%>
<%@page import="qldc.pm.tienan.searchcontainer.NguoiCoTienAnSearchTerms"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="java.util.List"%>

<%@include file="../../init.jsp"%>

<%
String pathThemeImages = themeDisplay.getPathThemeImages();
SimpleDateFormat dateFormat = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);

PortletURL portletURL = renderResponse.createRenderURL();
portletURL.setParameter("mvcPath", "/html/view.jsp");
portletURL.setParameter("chonChucNangItem", TienAnConstants.QUAN_LY_NGUOI_CO_TIEN_AN);
%>

<portlet:renderURL var="themMoiNguoiCoTienAnUrl">
	<portlet:param name="mvcPath" value="/html/view.jsp" />
	<portlet:param name="chonChucNangItem" value="<%=TienAnConstants.QUAN_LY_NGUOI_CO_TIEN_AN %>" />
	<portlet:param name="tabs1" value="cap_nhat_nguoi_co_tien_an" />
	<portlet:param name="redirect" value="<%=portletURL.toString() %>" />
</portlet:renderURL>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>

<script type="text/javascript">
var NhapNgay = (function() {
	function NhapNgay() {}
	/*
	 * 
	 */
	 NhapNgay.prototype.initCalendars = function() {
		$("#<portlet:namespace/>tuNgayPhamToi").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
				buttonImageOnly : true,
				buttonText : "<liferay-ui:message key="tuNgayPhamToi" />",
				dateFormat : "dd/mm/yy",
				onClose: function( selectedDate ) {
					 $("#<portlet:namespace/>denNgayPhamToi").datepicker( "option", "minDate", selectedDate );
					 }
			});
		$("#<portlet:namespace/>denNgayPhamToi").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
				buttonImageOnly : true,
				buttonText : "<liferay-ui:message key="denNgayPhamToi" />",
				dateFormat : "dd/mm/yy",
				onClose: function( selectedDate ) {
					 $("#<portlet:namespace/>tuNgayPhamToi").datepicker( "option", "maxDate", selectedDate );
					 }
			});
		
		
		$("#<portlet:namespace/>tuNgayToaAn").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="tuNgayTaoTrichLuc" />",
			dateFormat : "dd/mm/yy",
			onClose: function( selectedDate ) {
				$("#<portlet:namespace/>denNgayToaAn").datepicker( "option", "minDate", selectedDate );
			}
		});
		
		$("#<portlet:namespace/>denNgayToaAn").datepicker({
			showOn: "both",
			buttonImage: "<%=pathThemeImages%>/common/calendar.png",
			buttonImageOnly : true,
			buttonText : "<liferay-ui:message key="denNgayTaoTrichLuc" />",
			dateFormat : "dd/mm/yy",
			onClose: function( selectedDate ) {
				$("#<portlet:namespace/>tuNgayToaAn").datepicker( "option", "maxDate", selectedDate );
			}
		});
	};

	return NhapNgay;
})();

$(function() {
	var dom = new NhapNgay();

	dom.initCalendars();
});

function <portlet:namespace/>searchValidate() {
	var soCMND = $('#<portlet:namespace/>soCMND');
	if(soCMND.val() != '' && !validateIntegerWithLength(soCMND.val(),1,12)) {
		showPopupConfirm('<liferay-ui:message key="so-cmnd-nay-khong-hop-le" />', ['Ok'], function(index) {
			soCMND.focus();
		});
		
		return false;
	} else {
		var form = $("#<portlet:namespace/>fm");
		form.attr('onsubmit', "return true;");
		form.submit();
	}
}
</script>

<form action="<%=portletURL.toString()%>" method="POST" id="<portlet:namespace/>fm" name="<portlet:namespace/>fm" onsubmit="return false;">
<%
	NguoiCoTienAnSearch nguoiCoTienAnSearch = new NguoiCoTienAnSearch(renderRequest, portletURL);
	NguoiCoTienAnDisplayTerms nguoiCoTienAnDisplayTerms = (NguoiCoTienAnDisplayTerms) nguoiCoTienAnSearch.getDisplayTerms();
	NguoiCoTienAnSearchTerms nguoiCoTienAnSearchTerms = (NguoiCoTienAnSearchTerms) nguoiCoTienAnSearch.getSearchTerms();
	
	Tinh tinh = TinhLocalServiceUtil.findTinhMacDinh();
%>
	
	<table class="gridnormal">
		<tr>
			<td class="aui-w15"><liferay-ui:message key="so-dinh-danh-ca-nhan" /></td>
			<td class="aui-w35"><input type="text" name="<portlet:namespace/>soDinhDanhCaNhan" value="<%=nguoiCoTienAnDisplayTerms.getSoDinhDanhCaNhan()%>" /></td>
			<td class="aui-w15"><liferay-ui:message key="so-cmnd" /></td>
			<td class="aui-w35"><input type="text" id="<portlet:namespace/>soCMND" name="<portlet:namespace/>soCMND" value="<%=nguoiCoTienAnDisplayTerms.getSoCMND()%>" /></td>
		</tr>
		
		<tr>			
			<td><liferay-ui:message key="ho-va-ten" /></td>
			<td><input type="text" name="<portlet:namespace/>hoVaTen" value="<%=nguoiCoTienAnDisplayTerms.getHoVaTen()%>" /></td>
			<td><liferay-ui:message key="ban-an-so" /></td>
			<td><input type="text" name="<portlet:namespace/>banAnSo" value="<%=nguoiCoTienAnDisplayTerms.getBanAnSo()%>" /></td>
		</tr>

		<tr>
			<td><liferay-ui:message key="ngay-toa-an-tu" /></td>
			<td class="ip-datepicker">
				<input type="text" style="width: 90%;" id="<portlet:namespace/>tuNgayToaAn" name="<portlet:namespace/>tuNgayToaAn" value="<%=Validator.isNotNull(nguoiCoTienAnDisplayTerms.getTuNgayToaAn()) ? dateFormat.format(nguoiCoTienAnDisplayTerms.getTuNgayToaAn()) : "" %>" />
			</td>
			<td><liferay-ui:message key="den-ngay" /></td>
			<td class="ip-datepicker">
				<input style="width: 90%;" type="text" id="<portlet:namespace/>denNgayToaAn" name="<portlet:namespace/>denNgayToaAn" value="<%=Validator.isNotNull(nguoiCoTienAnDisplayTerms.getDenNgayToaAn()) ? dateFormat.format(nguoiCoTienAnDisplayTerms.getDenNgayToaAn()) : "" %>" />
			</td>
		</tr>
		
		<tr>
			<td><liferay-ui:message key="noi-thuong-tru" /></td>
			
			<td colspan="2">
				<select class="display-inline" style="width: 30%" disabled="disabled">
					<option selected="selected"><%=tinh.getTenTinh() %></option>
				</select>
				
				<select class="display-inline" style="width: 34%" name="<portlet:namespace/>noiThuongTruQuanHuyenId" id="<portlet:namespace/>noiThuongTruQuanHuyenId" onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=nguoiCoTienAnDisplayTerms.getNoiThuongTruPhuongXaId() %>');">
					<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>
					
					<%
					List<QuanHuyen> quanHuyens = QuanHuyenLocalServiceUtil.findByTinhId(tinh.getTinhId());
					
					for(QuanHuyen quanHuyen : quanHuyens) {
					%>
					<option value="<%=quanHuyen.getQuanHuyenId() %>" <%=quanHuyen.getQuanHuyenId() == nguoiCoTienAnDisplayTerms.getNoiThuongTruQuanHuyenId() ? "selected=selected" : "" %>><%=quanHuyen.getTenQuanHuyen() %></option>
					<%
					}
					%>
				</select>
				
				<select class="display-inline" style="width: 34%" name="<portlet:namespace/>noiThuongTruPhuongXaId" id="<portlet:namespace/>noiThuongTruPhuongXaId">
					<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>
				</select>
			</td>
			<td>
				<input type="text" name="<portlet:namespace/>noiThuongTruDiaChi" id="<portlet:namespace/>noiThuongTruDiaChi" value="<%=nguoiCoTienAnDisplayTerms.getNoiThuongTruDiaChi() %>" />
			</td>
		</tr>
		
		<tr>
			<td></td>
			<td colspan="3">
				<div align="left">
					<input type="submit" onclick="<portlet:namespace/>searchValidate();" class="btn btn-primary" value="<liferay-ui:message key="tim" />" />
					<input type="button" class="btn btn-danger" onclick="javascript:submitForm(document.hrefFm,'<%=themMoiNguoiCoTienAnUrl.toString() %>');" value="<liferay-ui:message key="them-moi-nguoi-co-tien-an" />" />
					<%
					TEMP_DoiTuongTienAn doiTuongTienAnTemp = TEMP_DoiTuongTienAnLocalServiceUtil.findTempDoiTuongTienAnMoiNhatByUserId(user.getUserId());
					
					if (Validator.isNotNull(doiTuongTienAnTemp)) {
						TEMP_TienAn tienAnTemp = TEMP_TienAnLocalServiceUtil.findTienAnMoitNhatOfDoiTuongTienAnIdTemp(doiTuongTienAnTemp.getDoiTuongTienAnId());
						
						if (Validator.isNotNull(tienAnTemp)) {
							if (tienAnTemp.getThaoTac().equals(Constants.DELETE)) {
								
					%>
					<portlet:renderURL var="undoXoaTienAnRenderURL">
						<portlet:param name="mvcPath" value="/html/view.jsp"/>
						<portlet:param name="chonChucNangItem" value="<%=TienAnConstants.QUAN_LY_NGUOI_CO_TIEN_AN %>"/>
						<portlet:param name="tabs1" value="cap_nhat_tien_an"/>
						<portlet:param name="soDinhDanhCaNhan" value="<%=doiTuongTienAnTemp.getSoDinhDanhCaNhan() %>" />
						<portlet:param name="doiTuongTienAnId" value="<%=String.valueOf(doiTuongTienAnTemp.getDoiTuongTienAnId()) %>"/>
						<portlet:param name="tienAnId" value="<%=String.valueOf(tienAnTemp.getTienAnId()) %>"/>
						<portlet:param name="thaoTac" value="<%=tienAnTemp.getThaoTac() %>"/>									
						<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
					</portlet:renderURL>
					<input type="button" class="btn btn-primary" onclick="javascript:submitForm(document.hrefFm,'<%=undoXoaTienAnRenderURL.toString()%>');" value="<liferay-ui:message key="un-do-xoa-tien-an" />" />
								
					<%
					} else {
						String key = "re-do-sua-tien-an";
						String undoRedo = ThaoTacUndoRedoEnum.REDO.getKey();
						if (Validator.isNull(tienAnTemp.getUndoRedo()) ||  tienAnTemp.getUndoRedo().equals(ThaoTacUndoRedoEnum.UNDO.getKey())) {
							undoRedo = ThaoTacUndoRedoEnum.UNDO.getKey();
							key = "un-do-sua-tien-an";
						}
					%>
				 	<portlet:renderURL var="undoSuaTienAnRenderURL">
						<portlet:param name="mvcPath" value="/html/view.jsp"/>
						<portlet:param name="chonChucNangItem" value="<%=TienAnConstants.QUAN_LY_NGUOI_CO_TIEN_AN %>"/>
						<portlet:param name="tabs1" value="cap_nhat_tien_an"/>
						<portlet:param name="soDinhDanhCaNhan" value="<%=doiTuongTienAnTemp.getSoDinhDanhCaNhan() %>" />
						<portlet:param name="doiTuongTienAnId" value="<%=String.valueOf(doiTuongTienAnTemp.getDoiTuongTienAnId()) %>"/>
						<portlet:param name="tienAnId" value="<%=String.valueOf(tienAnTemp.getTienAnId()) %>"/>
						<portlet:param name="undoRedo" value="<%=undoRedo %>"/>		
						<portlet:param name="thaoTac" value="<%=tienAnTemp.getThaoTac() %>"/>									
						<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
					</portlet:renderURL>
					<input type="button" class="btn btn-primary" onclick="javascript:submitForm(document.hrefFm,'<%=undoSuaTienAnRenderURL.toString() %>');" value="<liferay-ui:message key="<%=key %>" />" />
								 
					<%
							}
						}
					}
					%>
				</div>
			</td>
		</tr>
	</table>
</form>

<liferay-ui:search-container searchContainer="<%=nguoiCoTienAnSearch %>">
	<liferay-ui:search-container-results>
	<%
		results = DoiTuongTienAnDTOLocalServiceUtil.findBySDDCN_CMND_SSHK_HVT_DCTT(nguoiCoTienAnSearchTerms.getSoDinhDanhCaNhan(), nguoiCoTienAnSearchTerms.getSoCMND(), "", nguoiCoTienAnSearchTerms.getHoVaTen(), tinh.getTinhId(), nguoiCoTienAnSearchTerms.getNoiThuongTruQuanHuyenId(), nguoiCoTienAnSearchTerms.getNoiThuongTruPhuongXaId(), nguoiCoTienAnSearchTerms.getNoiThuongTruDiaChi(), null, null, nguoiCoTienAnSearchTerms.getBanAnSo(), nguoiCoTienAnSearchTerms.getTuNgayToaAn(), nguoiCoTienAnSearchTerms.getDenNgayToaAn(),TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue(), nguoiCoTienAnSearch.getStart(), nguoiCoTienAnSearch.getEnd(), nguoiCoTienAnSearch.getOrderByComparator());

		total = DoiTuongTienAnDTOLocalServiceUtil.countBySDDCN_CMND_SSHK_HVT_DCTT(nguoiCoTienAnSearchTerms.getSoDinhDanhCaNhan(), nguoiCoTienAnSearchTerms.getSoCMND(), "", nguoiCoTienAnSearchTerms.getHoVaTen(), tinh.getTinhId(), nguoiCoTienAnSearchTerms.getNoiThuongTruQuanHuyenId(), nguoiCoTienAnSearchTerms.getNoiThuongTruPhuongXaId(), nguoiCoTienAnSearchTerms.getNoiThuongTruDiaChi(), null, null, nguoiCoTienAnSearchTerms.getBanAnSo(), nguoiCoTienAnSearchTerms.getTuNgayToaAn(), nguoiCoTienAnSearchTerms.getDenNgayToaAn(),TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue());

		pageContext.setAttribute("results", results);
		pageContext.setAttribute("total", total);
	%>
	</liferay-ui:search-container-results>
	
	<%
	int start = nguoiCoTienAnSearch.getDelta() * (nguoiCoTienAnSearch.getCur() - 1);
	%>
	
	<liferay-ui:search-container-row className="qldc.pm.tienan.dto.DoiTuongTienAnDTO" keyProperty="doiTuongTienAnId" modelVar="doiTuongTienAn">
		<liferay-ui:search-container-row-parameter name="soDinhDanhCaNhan" value="<%=doiTuongTienAn.getSoDinhDanhCaNhan() %>" />
	
	<%
	String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(doiTuongTienAn.getNoiThuongTruQuanHuyenId(), doiTuongTienAn.getNoiThuongTruPhuongXaId(), doiTuongTienAn.getNoiThuongTruDiaChi());
	%>
	
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start) %>" />
		
		<liferay-ui:search-container-column-text cssClass="aui-w15" name="ho-va-ten" property="hoVaTen" orderable="<%=true %>" orderableProperty="hoVaTen" />
		
		<liferay-ui:search-container-column-text cssClass="txt-center " name="so-dinh-danh-ca-nhan" property="soDinhDanhCaNhan" orderable="<%=true %>" orderableProperty="soDinhDanhCaNhan" />
		
		<liferay-ui:search-container-column-text cssClass="txt-center" name="so-cmnd" property="soCMND" orderable="<%=true %>" orderableProperty="soCMND" />
		
		<liferay-ui:search-container-column-text name="noi-thuong-tru" value="<%=diaChiThuongTru %>" />
		
		<liferay-ui:search-container-column-text cssClass="txt-center"  name="ngay-toa-an" property="ngayToaAn" />
 		
 		<liferay-ui:search-container-column-text cssClass="txt-center" name="ban-an-so" property="banAnSo" />
		
		<liferay-ui:search-container-column-jsp cssClass="txt-center aui-Action" path="/html/tienan/nguoi_co_tien_an/danh_sach_actions.jsp" align="right" />
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator searchContainer="<%=nguoiCoTienAnSearch %>" />
</liferay-ui:search-container>

<script type="text/javascript">
$(function() {
	 loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=nguoiCoTienAnDisplayTerms.getNoiThuongTruPhuongXaId() %>');
});
</script>